package com.google.donutrush;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Entities.RTentry;
import Entities.User;
import Services.RTcomparator;
import Services.UserService;
import Services.UserServiceDBimpl;

import com.google.appengine.repackaged.org.json.JSONArray;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

public class ResponseServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(ResponseServlet.class.getName());

	public void doPost(HttpServletRequest request, HttpServletResponse response){
		log.info("responseServlet called");
		UserService us = UserServiceDBimpl.getInstance();
		
		log.info("us.getRT() called");
		List highscores = us.getRT();
		 
		Comparator comparator = new RTcomparator();//I use this comparator to sort the list
		Collections.sort(highscores,comparator);
		
		//send only the top 10
		int sizeOfRt = highscores.size();
		for(int i=10;i < sizeOfRt;i++){
			highscores.remove(10);
		}
		
		log.info("Length of RT: "+highscores.size());
		//creating the json to be sent to client
		JSONArray arrayObj=new JSONArray();
		
		
		
		//this is the way I fill in the json RT, go over the list adding the rank to user name and adding the object to the json
		int i = 1;
		int score = 0;
		String name = null;
		String facebookID = null;
		int userScore = 0;
		
		//preparing the HighScore Table
		for (Object rte : highscores) {
			name = ((RTentry)rte).getName();
			score = ((RTentry)rte).getScore();
			facebookID = ((RTentry)rte).getFbID();
			JSONObject jsonObject = new JSONObject();
			
			try {
				jsonObject.put("name", name);//adding to json the string name and it's value score
				jsonObject.put("score", score);
				jsonObject.put("fbID", facebookID);
				
				
				
				//System.out.println(score);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			arrayObj.put(jsonObject);
			}
		
		//retrieving information for set-cookies
		User Playinguser = (User) request.getAttribute("newUser");
		User user = us.getUserByEmail(Playinguser.getEmail());
		if(user!=null){
			userScore = user.getBestScore();
			name = user.getName();
		}
		else{
			userScore = Playinguser.getBestScore();
			name = Playinguser.getName();
			if (name=="") name="Anonymous";
		}
			
		
		
		
		//setting a cookie in the response
		response.addCookie(new Cookie("nick" , name));
		response.addCookie(new Cookie("best-score" , Integer.toString(userScore)));
		

		
		//from here is just the way to sent the json back to the client
		response.setContentType("text/plain");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String myString = arrayObj.toString();
		writer.println(myString);
		writer.close();
		//and.. sent

	}
	
}
